﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using HR60_PanelSoft.View.Base;

namespace HR60_PanelSoft.View.TabForms.ListItems
{
	/// <summary>
	/// 面板按钮分页控件
	/// </summary>
	public partial class ButtonPage : BaseData
	{
		public ButtonPage()
		{
			InitializeComponent();
			ButtonCountPerPage = 8;
			PageCount = 8;
		}

		private int pageCount = 8;
		private int pageNum = 1;
		private int buttonCountPerPage = 8;
		private int selectedButtonNum = 0;

		/// <summary>
		/// 翻页事件
		/// </summary>
		[Browsable(true)]
		public event EventHandler PageChanged;

		/// <summary>
		/// 按钮选中事件
		/// </summary>
		[Browsable(true)]
		public event EventHandler ButtonSelected;

		[Browsable(true)]
		[Description("标题"), Category("String"), DefaultValue("标题")]
		public string Title
		{
			get
			{
				return ltlTitle.Text;
			}
			set
			{
				ltlTitle.Text = value;
			}
		}

		[Browsable(true)]
		[Description("最大页码"), Category("Int32"), DefaultValue(8)]
		public int PageCount
		{
			get
			{
				return pageCount;
			}
			set
			{
				ltlPageNum.Text = "1/" + value;
				pageCount = value;
			}
		}

		[Browsable(true)]
		[Description("每页的按钮数量"), Category("Int32"), DefaultValue(8)]
		public int ButtonCountPerPage
		{
			get
			{
				return this.buttonCountPerPage;
			}
			set
			{
				switch (value)
				{
					case 8:
						setTableFrame(2, 4);
						break;
					case 4:
						setTableFrame(2, 2);
						break;
					default:
						setTableFrame(1, value);
						break;
				}
				buttonCountPerPage = value;
			}
		}

		/// <summary>
		/// 页码
		/// </summary>
		public int PageNum
		{
			get
			{
				return pageNum;
			}
		}

		/// <summary>
		/// 已选择的按钮编号
		/// </summary>
		public int SelectedButtonNum
		{
			get
			{
				return selectedButtonNum;
			}
		}

		/// <summary>
		/// 设置表格框架
		/// </summary>
		/// <param name="row">行</param>
		/// <param name="col">列</param>
		private void setTableFrame(int row, int col)
		{
			buttonTable.Controls.Clear();
			buttonTable.RowCount = row;
			buttonTable.ColumnCount = col;
			int k = 0;
			for(int i = 0; i<row; i++)
			{
				for(int j =0; j<col; j++)
				{
					Label ltlText = new Label();
					ltlText.Margin = new Padding(0);
					ltlText.Dock = DockStyle.Fill;
					ltlText.TextAlign = ContentAlignment.MiddleCenter;
					ltlText.Text = ++k + "-未定义";
					ltlText.Name = "ltl" + k;
					ltlText.Tag = k;
					buttonTable.Controls.Add(ltlText, j, i);

					ltlText.Click += new EventHandler(ltlText_Click);
				}
			}
			
		}

		/// <summary>
		/// 设置表格单元文字
		/// </summary>
		/// <param name="index">编号,从1开始</param>
		/// <param name="text">文字</param>
		public void setTableCellText(int index, string text)
		{
			buttonTable.Controls["ltl" + index].Text = index + "-" + text;
		}

		/// <summary>
		/// 添加项
		/// </summary>
		/// <param name="item"></param>
		public void appendItemControl(Control item)
		{
			panelList.Controls.Add(item);
		}

		/// <summary>
		/// 清除项
		/// </summary>
		public void cleanItemControl()
		{
			panelList.Controls.Clear();
		}

		/// <summary>
		/// 设置按钮状态
		/// </summary>
		/// <param name="btnNum"></param>
		/// <param name="selected"></param>
		private void setButtonStatus(int btnNum, bool selected)
		{
			if (btnNum == 0) return;
			Label ltl = (Label)buttonTable.Controls["ltl" + btnNum];
			if (selected)
			{
				ltl.BackColor = Color.Orange;
				ltl.ForeColor = Color.FromArgb(0xfc, 0xfc, 0xfc);
			}
			else
			{
				if (ltl != null)
				{
					ltl.BackColor = Color.Transparent;
					ltl.ForeColor = Color.Black;
				}
			}
		}

		#region 控件事件

		// 按钮选择事件
		void ltlText_Click(object sender, EventArgs e)
		{
			Label ltl = (Label)sender;
			int btnNum = (int)ltl.Tag;

			if (btnNum == selectedButtonNum)
			{
				setButtonStatus(selectedButtonNum, false);
				selectedButtonNum = 0;
			}
			else
			{
				setButtonStatus(selectedButtonNum, false);
				setButtonStatus(btnNum, true);
				selectedButtonNum = btnNum;
			}
			if (ButtonSelected != null) ButtonSelected(this, e);
		}

		// 上一页
		private void btnPrev_Click(object sender, EventArgs e)
		{
			if (pageNum == 1) return;
			pageNum--;
			ltlPageNum.Text = pageNum + "/" + pageCount;
			if(PageChanged != null)
				PageChanged(this, e);

			// 翻页时按钮选择复位
            if (selectedButtonNum != 0)
            {
                setButtonStatus(selectedButtonNum, false);
                selectedButtonNum = 0;
                if (ButtonSelected != null) ButtonSelected(this, e);
            }
		}

		// 下一页
		private void btnNext_Click(object sender, EventArgs e)
		{
			if (pageNum == pageCount) return;

			    pageNum++;
			    ltlPageNum.Text = pageNum + "/" + pageCount;
			if (PageChanged != null)
				PageChanged(this, e);

			// 翻页时按钮选择复位
            if (selectedButtonNum != 0)
            {
                setButtonStatus(selectedButtonNum, false);
                selectedButtonNum = 0;
                if (ButtonSelected != null) ButtonSelected(this, e);
            }
		}

		#endregion

	}
}
